<?php

class Model_Category
{
    private   $sys;
    public    $config;
    protected $table_name;
    
    public function __construct()
    {
        $this->sys        = new Model_SiteManage();
        $config           = __get_config('plugin.ini');
        $this->config     = $config['MANAGE_CATEGORY'];
        $this->table_name = 'M_CATEGORY';
    }
    //
    public function insert($data)
    {
        $db = new Model_DB();
        return $db->insert($this->table_name, $data);
    }
    public function update($id, $data)
    {
        $db = new Model_DB();
        return $db->update($id, $this->table_name, $data);
    }
    public function get($id)
    {
        $db = new Model_DB();
        $sql = sprintf("select * from {$this->table_name} where site_id = %d and id = %d", $this->sys->getCurrentSite(), $db->escape($id));
        $db->query($sql);
        return $db->fetch();
    }
    public function delete($id)
    {
        $db   = new Model_DB();
        $data = array();
        $data['id']      = $id;
        $data['site_id'] = $this->sys->getCurrentSite();
        return $db->delete($this->table_name, $data);
    }
    public function getListCount($data)
    {
        $db    = new Model_DB();
        $where = $this->createSearchCondition($data);
        $sql   = "select count(*) from ".$this->table_name;
        $sql  .= (!empty($where) ? " where ".implode(' AND ', $where) : "");
        $db->query($sql);
        if ($db->res){
            return array_shift($res = $db->fetch());
        }
        return false;
    }
    public function getList($data, $orderby = 'id asc', $limit = '', $offset = '')
    {
        $list  = array();
        $db    = new Model_DB();
        $where = $this->createSearchCondition($data);
        $sql   = "select * from ".$this->table_name." ";
        $sql  .= (!empty($where) ? " where ".implode(' AND ', $where) : "") . (!empty($orderby) ? " order by {$orderby}" : "") . (!empty($limit) ? " limit {$limit}" : "") . (!empty($offset) ? " offset {$offset}" : "");
        $db->query($sql);
        if ($db->res){
            while($res = $db->fetch()){
                if(isset($res['id'])){
                    $list[$res['id']] = $res;
                }
                else{
                    $list[] = $res;
                }
            }
        }
        return $list;
    }
    private function createSearchCondition($data, $prefix = '')
    {
        $db    = new Model_DB();
        $where = array();
        if ($prefix != ''){
            $prefix = $prefix . '.';
        }
        $where[] = "{$prefix}site_id = ". $this->sys->getCurrentSite();
        foreach($db->getFieldList($this->table_name) as $key => $_){
            if (isset($data[$key]) && $data[$key] != ''){
                $value = $db->escape($data[$key]);
                if ($key == 'category_name'){
                    $where[] = "{$prefix}{$key} collate utf8_unicode_ci like '%{$value}%'";
                }else{
                    $where[] = "{$prefix}{$key} = '{$value}'";
                }
            }
        }
        return $where;
    }
    public function validate(&$data, &$err)
    {
        $validate = new Model_Validate();
        $keys = explode(',', 'category_name');
        foreach($keys as $key){
            if (!isset($data[$key]) || $validate->NotEmpty($data[$key])){
                $err[$key] = $validate->getMessages("NotEmpty");
            }
        }
        return empty($err);
    }
    //
    public function getFormKeys()
    {
        $db = new Model_DB();
        return array_keys($db->getFieldList($this->table_name));
    }
    public function get_ID()
    {
    	$db = new Model_DB;
    	$db->query( "SELECT COALESCE( MAX(id), 1) AS max_id FROM ".$this->table_name );
    	$row = $db->fetch();
    	return $row['max_id'] + 1;
    }
}
?>